<?php
namespace App\Http\Controllers\Admin;
use App\Http\Model\User;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class IndexController extends CommonController{
    public function index(){
        return view('admin.index');
    }
    public function login(){
        session(['admin'=>1]);
        echo '<h1>登录</h1>';
    }
    public function info(){
        return view('admin.info');
    }
    public function pass(){
        if($input= Input::all()){
            $rules=array(
                'password'=>'required|between:6,20|confirmed',
            );
            $msg=[
                'password.required'=>'新密码不能为空',
                'password.between'=>'密码长度为6-20',
                'password.confirmed'=>'两次密码不一致'
            ];
            $validator = Validator::make($input,$rules,$msg);
            if($validator->passes()){
                $user_session = session('user');
                $user = User::where('user_id',$user_session['user_id'])->first();
                if(Crypt::decrypt($user['password']) != $input['password_o'] ){
                    return back()->with('errors','原密码错误');
                }else{
                    $user->password=Crypt::encrypt($input['password']);
                    $user->update();
                    return redirect('admin/info');
                }
            }else{
                return back()->withErrors($validator);
            }
            //原密码


         echo 'ok';

        }
        return view('admin.pass');
    }
}